package com.mzy.concurrent;

/**
 * @Author: mizy3
 * @Date: 2024/08/20/下午3:20
 * @Description:
 */
public class 交替打印A1b2c3 {

    public static final Object lock = new Object();
    public static boolean letter = false;

    public static void main(String[] args) {
        new Thread(() -> {
            for (char i = 'A'; i <= 'C'; i++) {
                synchronized (lock) {
                    while (letter) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print(i);
                    letter = true;
                    lock.notifyAll();
                }
            }
        }).start();

        new Thread(() -> {
            for (int i = 1; i <= 3; i++) {
                synchronized (lock){
                    while (!letter){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print(i);
                    letter = false;
                    lock.notifyAll();
                }
            }
        }).start();


    }
}
